package com.firesrc.mail.send;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.firesrc.bean.MailContent;
import com.firesrc.bean.MailSubject;
import com.firesrc.bean.Record;
import com.firesrc.service.MailContentService;
import com.firesrc.service.MailSubjectService;
import com.firesrc.service.impl.MailContentServiceImpl;
import com.firesrc.service.impl.MailSubjectServiceImpl;
import com.firesrc.util.JsoupUtil;
import com.firesrc.util.SystemUtil;

public class MailHelper {

	private static List<String> subjectList = new ArrayList<String>();
	private static List<String> bodyList = new ArrayList<String>();
	private static Map<String, String> map = new HashMap<String, String>();

	public static String getSubject(Record record) {
		String string = getSubjectString();
		map.put("keyword", record.getKeyword());
		map.put("date", SystemUtil.getCurrentDateStringYYYYMMDD());
		map.put("url", record.getUrl());
		map.put("time", (new Date()).toString());
		string = JsoupUtil.getTextFromTemplate(string, map);
		return string;
	}

	private static String getSubjectString() {
		if (subjectList.size() == 0) {
			initSubjectList();
		}
		int b = (int) (Math.random() * subjectList.size());
		return subjectList.get(b);
	}

	private static void initSubjectList() {
		MailSubjectService service = new MailSubjectServiceImpl();
		List<MailSubject> allList = service.getAllList();
		for (MailSubject mailSubject : allList) {
			subjectList.add(mailSubject.getName());
		}
	}

	public static String getMailText() {
		return "测试代码  的说法撒摄影,关注豆腐干度 的技术斯蒂三位,";
	}

	public static String getMailHtml(Record record) {
		if (bodyList.size() == 0) {
			initBodyList();
		}
		int b = (int) (Math.random() * bodyList.size());
		String string = bodyList.get(b);

		map.put("keyword", record.getKeyword());
		map.put("date", SystemUtil.getCurrentDateStringYYYYMMDD());
		map.put("url", record.getUrl());
		map.put("time", (new Date()).toString());
		string = JsoupUtil.getTextFromTemplate(string, map);
		return string;
	}

	public static String getMailText(Record record) {
		if (bodyList.size() == 0) {
			initBodyList();
		}
		int b = (int) (Math.random() * bodyList.size());
		String string = bodyList.get(b);

		map.put("keyword", record.getKeyword());
		map.put("date", SystemUtil.getCurrentDateStringYYYYMMDD());
		map.put("url", record.getUrl());
		map.put("time", (new Date()).toString());
		string = JsoupUtil.getTextFromTemplate(string, map);
		return string;
	}

	private static void initBodyList() {
		//		bodyList.add(JsoupUtil.getMailBodyHtml("mailcontent.html"));
		//		bodyList.add(JsoupUtil.getMailBodyHtml("mailcontent1.html"));
		//		bodyList.add(JsoupUtil.getMailBodyHtml("mailcontent2.html"));
		//		bodyList.add(JsoupUtil.getMailBodyHtml("mailcontent3.html"));
		//		bodyList.add(JsoupUtil.getMailBodyHtml("mailcontent4.html"));
		MailContentService service = new MailContentServiceImpl();
		List<MailContent> allList = service.getAllList();
		for (MailContent mailContent : allList) {
			bodyList.add(mailContent.getText());
		}
	}

	public void insertToDb() {
		subjectList.add(" ${time}完美超越百度竞价，${keyword} 自然排名迅速升到首页第一！");
		subjectList.add("${keyword} 百度排名前三真的很难？${url} 24小时到第一！");
		subjectList.add(" ${url} 百度快速 ${keyword} 排名前三的秘笈，诚信交易！");
		subjectList.add("${keyword}百度.${url} 排名前二十到百度第一仅仅24小时！");
		subjectList.add("${url} 快速提高网站排名，几个小时 ${keyword}   排名到第一让你Hold不住！");
		subjectList.add("${keyword} 完美超越百 度竞 价，${url}自然排名迅速升到首页第一！");
		subjectList.add("${date} 担保付款，${url} 百度自然排名从二十名到第一名不是神话！");
		subjectList.add("${date}  ${url} 纯  白帽 优化,<${keyword}> 最快一 天排名.进前三");
		MailSubjectService service = new MailSubjectServiceImpl();
		for (String name : subjectList) {
			MailSubject subject = new MailSubject();
			subject.setId(UUID.randomUUID().toString());
			subject.setName(name);
			service.add(subject);

		}

	}
}
